/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


goog.provide('kramerius.audio.ui.Timeline');

goog.require('kramerius.audio.ui.PlayerComponent');
goog.require('goog.dom');
goog.require('goog.ui.Tooltip');
goog.require('goog.ui.Slider');


/**
 * @constructor 
 * @param {Element} container Element containing this component.
 * @param {kramerius.audio.Controller} controller Controller of the audio player.
 */
kramerius.audio.ui.Timeline = function(container, controller){
    kramerius.audio.ui.PlayerComponent.call(this, container, controller);
    this.controller_.setProgressBar(this);
    this.slider_ = this.initializeSlider_();
    this.disable();
};
goog.inherits(kramerius.audio.ui.Timeline, kramerius.audio.ui.PlayerComponent);


/** @private **/
kramerius.audio.ui.Timeline.prototype.initializeSlider_ = function(){
    var slider =  new goog.ui.Slider();
    //slider.decorate(this.container_);
    slider.setOrientation(goog.ui.Slider.Orientation.HORIZONTAL);
    slider.setMoveToPointEnabled(true);
    var player = this.controller_;
    
    slider.addEventListener(goog.ui.Component.EventType.CHANGE, function(e) {
        player.setPosition(slider.getValue());
    //console.log("ProgressBar: updating position");
    });
    slider.createDom();
    var sliderEl = slider.getElement();
    this.toolTip_ = new goog.ui.Tooltip(sliderEl, "nastavit pozici");
    //kramerius.audio.debug_utils.appendDebugListeners(slider, "progress Bar");
    slider.render(this.container_);
    return slider;
};


kramerius.audio.ui.Timeline.prototype.enable = function(){
    this.slider_.setEnabled(true);
};


kramerius.audio.ui.Timeline.prototype.disable = function(){
    this.slider_.setEnabled(false);
};


/**
 * @param {number} length Total length of active track in milliseconds.
 */
kramerius.audio.ui.Timeline.prototype.setTotalLength = function(length){
    this.slider_.setMaximum(length);
};


/**
 * Updates slider position if it differs from actual position by at least 100 ms
 * @param {num} position Position in milliseconds.
 */
kramerius.audio.ui.Timeline.prototype.setPosition = function(position){
    if (kramerius.audio.utils.differsByAtLeast(this.slider_.getValue(), position, 100)){
        this.slider_.setValue(position);
    }
};
